package com.designoutput.ui
{
	import com.designoutput.enum.Direction;
	import com.designoutput.event.ScrollEvent;
	import com.designoutput.event.SliderEvent;
	import com.designoutput.ui.controls.Button;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.gallery.ImageHolder;
	import com.designoutput.ui.newControls.DialogBox;
	import com.designoutput.ui.newControls.Scrollbar;
	import com.designoutput.ui.newControls.Slider;
	import com.designoutput.utils.Globals;
	import com.designoutput.vo.GalleryImageVo;
	
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	[Event(name="complete", type="flash.events.Event")]
	public class ImageHolderDialog extends DialogBox
	{
		private const PADDING:Number = 20;
		private const TITLE_HEIGHT:Number = 20;
		private const GAP:Number = 10;
		private const FACTOR:Number = 300;		
		private const SCROLL_WIDTH:Number = 20; 
		private var _invalidScroll:Boolean = false;
		private var _zoomLabel:Literal;
		private var _zoomSlider:Slider;
		private var _imageItem:ImageHolder;
		private var _selectButton:Button;
		private var _closeButton:Button;
		private var _imageVo:GalleryImageVo;
		private var _parentCanvas:GalleryCanvas;
		private var _hscroll:Scrollbar;
		private var _vscroll:Scrollbar;
		private var _imgHeight:Number;
		private var _imgWidth:Number;
		private var _content:Sprite;		
		private var _imageActualHeight:Number;
		private var _imageActualWidth:Number;
		
		override protected function initialize():void {			
			super.initialize();
			
			_content = new Sprite();
			_content.x = PADDING;
			_content.y = PADDING + TITLE_HEIGHT;			
			addChild(_content);
			
			setTitle("Image Details");
			
			_zoomLabel = new Literal("Zoom:", true);
			addChild(_zoomLabel);
			
			_zoomSlider = new Slider(100, 100, 100, 400);		
			_zoomSlider.addEventListener(SliderEvent.DRAG, this.ZoomDrag, false, 0, true);
			addChild(_zoomSlider);			
			
			_vscroll = new Scrollbar(Direction.Vertical);
			_vscroll.addEventListener(ScrollEvent.SCROLL, this.verticalScroll, false, 0, true);
			addChild(_vscroll);
			
			_hscroll = new Scrollbar(Direction.Horizontal);
			_hscroll.addEventListener(ScrollEvent.SCROLL, this.horizontalScroll, false, 0, true);
			addChild(_hscroll);
			
			_imageItem = new ImageHolder();		
			_imageItem.height = Globals.designoutputCard.stage.stageHeight - 160;
			_imageItem.width = Globals.designoutputCard.stage.stageWidth - 110;			
			//_imageItem.addEventListener(MouseEvent.CLICK, item_clickHandler, false, 0, true);
			_imageItem.addEventListener("ImageLoaded", imageLoadedHandler);			
			_content.addChild(_imageItem);
			
			_selectButton = new Button("Select", 100);
			_selectButton.addEventListener(MouseEvent.CLICK, selectButton_clickHandler, false, 0, true);
			addChild(_selectButton);
			
			_closeButton = new Button("Close", 100);
			_closeButton.addEventListener(MouseEvent.CLICK, closeButton_clickHandler, false, 0, true);
			addChild(_closeButton);			
			
			width = Globals.designoutputCard.stage.stageWidth - 50;
			height = Globals.designoutputCard.stage.stageHeight - 50;
		}		
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			var contentAvailableWidth:Number =  _imageItem.width;
			var contentAvailableHeight:Number = _imageItem.height;
			var scrollContentSize:Point = getScrollContentSize();	
			
			_imageItem.x = 0;
			_imageItem.y = 0;
			
			_vscroll.viewportSize = _imageItem.height;
			_vscroll.size = _imageItem.height;
			_vscroll.contentSize = scrollContentSize.y;
			_vscroll.x = _imageItem.x + _imageItem.width + PADDING;
			_vscroll.y = 20 + TITLE_HEIGHT;	
			
			_hscroll.x = 20;
			_hscroll.y = _imageItem.height + PADDING + TITLE_HEIGHT;
			_hscroll.viewportSize = _imageItem.width;
			_hscroll.size = _imageItem.width;
			_hscroll.contentSize = scrollContentSize.x;		
			
			_zoomLabel.x = 20;
			_zoomLabel.y = _hscroll.y + _hscroll.height + GAP;
			
			_zoomSlider.x = _zoomLabel.x + _zoomLabel.width + GAP;
			_zoomSlider.y = _hscroll.y + _hscroll.height + GAP;
			
			_selectButton.x = (_width/2) - (_selectButton.width + 5);
			_selectButton.y = _height - _selectButton.height - 20;
			
			_closeButton.x = (_width/2) + 5;
			_closeButton.y = _height - _closeButton.height - 20;
			
			applyScroll();
			drawBorder();
		}
		
		protected function getScrollContentSize():Point {
			if(_imageItem && _imageItem.imageBody)				
				return new Point((_imageItem.imageBody.scaleX*_imageActualWidth), _imageItem.imageBody.scaleY*_imageActualHeight);
			
			return new Point();
		}
		
		private function drawBorder():void {
			var borderWidth:Number = 2;
			this.graphics.clear();
			this.graphics.lineStyle(borderWidth, 0xFFFFFF);
			this.graphics.drawRect(PADDING-borderWidth, PADDING + TITLE_HEIGHT - borderWidth, Globals.designoutputCard.stage.stageWidth - 110 + SCROLL_WIDTH, Globals.designoutputCard.stage.stageHeight - 160 + SCROLL_WIDTH);						
		}
		
		private function item_clickHandler(e:MouseEvent):void
		{			
			parentCanvas.onChangeImage(_imageVo);		
			close();
		}
		
		private function ZoomDrag(evt:SliderEvent) : void
		{	
			_imageItem.imageBody.scaleX = _zoomSlider.getValue()/FACTOR;			
			_imageItem.imageBody.scaleY = _zoomSlider.getValue()/FACTOR;
			_imageItem.updateImageBody();
			invalidateDisplay();
			applyScroll();
		}		
		
		protected function closeButton_clickHandler(e:MouseEvent):void {						
			close();
		}
		
		protected function selectButton_clickHandler(e:MouseEvent):void {
			parentCanvas.onChangeImage(_imageVo);		
			close();
		}	
		
		override protected function onCloseClick():void {
			close();
		}
		
		override protected function onConfirmKey():void {
			closeButton_clickHandler(null);
		}
		
		override protected function onCancelKey():void {
			onCloseClick();
		}
		
		private function horizontalScroll(evt:ScrollEvent):void
		{
			_invalidScroll = true;
			invalidateProperties();
		}
		
		private function verticalScroll(evt:ScrollEvent):void
		{
			_invalidScroll = true;
			invalidateProperties();
		}
		
		public function setImageDimension(nHeight:Number, nWidth:Number):void {
			_imgHeight = nHeight;
			_imgWidth = nWidth;
		}
		
		public function get imageVo():GalleryImageVo
		{
			return _imageVo;
		}
		
		public function set imageVo(value:GalleryImageVo):void
		{
			_imageVo = value;
			_imageItem.imageVo = value;
		}
		
		private function imageLoadedHandler(event:DataEvent):void {			
			_imageItem.removeEventListener("ImageLoaded", imageLoadedHandler);			
			
			var arr:Array = (event.data as String).split("_");
			
			_imageActualHeight = parseInt(arr[0]);
			_imageActualWidth = parseInt(arr[1]);
			
			_imageItem.maxScaleY = _imageItem.height / parseInt(arr[0]); 
			_imageItem.maxScaleX = _imageItem.width / parseInt(arr[1]);
			
			_imageItem.imageBody.scaleX = _imageItem.imageBody.scaleY = _zoomSlider.getValue()/FACTOR; 
			invalidateDisplay();	
		}
		
		public function get parentCanvas():GalleryCanvas
		{
			return _parentCanvas;
		}
		
		public function set parentCanvas(value:GalleryCanvas):void
		{
			_parentCanvas = value;
		}
		
		protected function applyScroll():void {	
			var rect:Rectangle = _content.scrollRect || new Rectangle();
			
			rect.x = _hscroll.scrollAmount;
			rect.y = _vscroll.scrollAmount;
			
			rect.width = _hscroll.viewportSize;
			rect.height = _vscroll.viewportSize;
			
			_content.scrollRect = rect;			
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			
			if(_invalidScroll){
				_invalidScroll = false;
				applyScroll();
			}
		}
	}
}